
from game.object.world.world import *
from game.object.world.cell import *
import os.path
import random

WORLD_DIR = "C:/world"

class DummyWorld(World):
    def __init__(self):
        self.earth_level = [1, #1000km
                            1, #100km
                            1, #10km
                            0, #1km
                            0, #100m
                            1, #10m
                            3, #1m
                            0];

    def load_block(self, coord, coord_depth, lod):
        """
        See def in World
        """
        cell = self.load_cell_from_file(coord, coord_depth,lod)
        loaded_from_file = True
        
        if(cell == None):
            print "Not loaded from file"
            cell = Cell(coord_depth, None)
            loaded_from_file = False
        
            if( self.check_below_group(coord, coord_depth) ):
                cell.terrain = 1
            else:
                cell.terrain = 0;
                
        d3 = coord_depth*3
        cell.x = coord[d3]
        cell.y = coord[d3+1]
        cell.z = coord[d3+2]
#        if(coord_depth == 5 and lod > 0):
#            cell = self.load_cell_from_file(coord, coord_depth)

#        d3 += 3
#        if( coord_depth < 6 and  lod > 0):
#            tempCoord = coord[:]
#            for x in xrange(CELL_WIDTH):
#                tempCoord[d3] = coord[d3]+x
#                for y in xrange(CELL_HEIGHT):
#                    tempCoord[d3+1] = coord[d3+1]+y
#                    for z in xrange(CELL_DEPTH):
#                        tempCoord[d3+2] = coord[d3+2]+z
#                        if( self.check_below_group(tempCoord, coord_depth+1) ):
#                            tcell = self.load_block(tempCoord, coord_depth+1, lod-1)
#                            tcell.terrain = 1
#                            cell.set_block(x, y, z, tcell)
#            if(cell.count == 200):
#                c = cell.first_child
#                while(c != None):
#                    print c.x, c.y, c.z
#                    c = c.next

                #print coord_depth
        if(not loaded_from_file):
            self.save_to_file(coord, coord_depth, cell)
            
        if(cell.count > 0):
            print " Count - ", cell.count
        return cell;

    def load_cell_from_file(self, coord, coord_depth, lod):
        dir = WORLD_DIR + self.build_cell_dir(coord, coord_depth) + "/cell.meta"
        if(os.path.exists(dir)):
            cell = Cell(coord_depth, None)
            with open(dir, 'r') as f:
                cell.terrain = f.readline()
                print cell.terrain
            return cell
        
        return None

    def save_to_file(self, coord, coord_depth, cell):
        dir = WORLD_DIR + self.build_cell_dir(coord, coord_depth)
        if( not os.path.exists(dir)):
            os.makedirs(dir)
        with open(dir + "/cell.meta", 'w') as f:
            f.write(str(cell.terrain))
        
        with open(dir + "/cell.data",'w') as f:
            for i in xrange(CELL_WIDTH*CELL_HEIGHT*CELL_DEPTH):
                if(i > 0 and i%10==0):
                    f.write('\n')
                if(cell.blocks[i] is None):
                    f.write('0')
                else:
                    f.write(str(cell.blocks[i].terrain))















                

    def check_below_group(self, coord, coord_depth):
        return random.randint(0,5) == 0
#        d = 0
#        while( d <= coord_depth and d < 7):
#            di = d*3+1
#            if(coord[di] > self.earth_level[d]):
#                return False;
#            if(coord[di] < self.earth_level[d]):
#                return True;
#            d += 1
#
#        return False;

    def build_cell_dir(self, coord, coord_depth):
        dir = "";
        for d in xrange(coord_depth+1):
            d3 = d*3
            dir += '/%03d-%03d-%03d' % (coord[d3],coord[d3+1],coord[d3+2])
        return dir